import { Component, OnInit, Input, OnDestroy } from "@angular/core";
// import { ConsoleReporter } from 'jasmine';
@Component({
  selector: "app-voice",
  templateUrl: "./voice.component.html",
  styleUrls: ["./voice.component.scss"]
})
export class VoiceComponent implements OnInit, OnDestroy {
  @Input() tagData: any;
  constructor() {}

  storeText: any;
  isSpeak = false; // 是否正在播放

  ngOnInit() {}

  playTextVoice() {
    const text = localStorage.getItem("voiceText");
    if (text) {
      //  文字合成语音
      const msg = new SpeechSynthesisUtterance(text);
      if (!this.isSpeak) {
        this.isSpeak = true;
        // 播放语音
        window.speechSynthesis.speak(msg);
        window.speechSynthesis.resume();
      } else {
        //  暂停语音
        this.isSpeak = false;
        window.speechSynthesis.pause();
      }
      msg.onend = () => {
        console.log("end");
        this.isSpeak = false;
        // 移除所有语音谈话队列中的谈话
        window.speechSynthesis.cancel();
      };
    }
  }
  ngOnDestroy() {
    if (window.speechSynthesis) {
      //  移除所有语音谈话队列中的谈话
      window.speechSynthesis.cancel();
    }
  }
}
